package com.crm.controller;

import com.crm.DTO.ContractDto;
import com.crm.common.result.ResultVO;
import com.crm.common.result.TableDataInfo;
import com.crm.common.utils.MinioUtil;
import com.crm.entity.Activity;
import com.crm.entity.Contract;
import com.crm.service.ContractService;
import com.crm.vo.ContractVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 合同
 */
@RestController
@Slf4j
@RequestMapping("/contract")
public class ContractController {
    @Autowired
    private ContractService contractService;

    @Autowired
    private MinioUtil minioUtil;

    /**
     * 上传合同文件
     */
    @PostMapping("/upload")
    public ResultVO<String> uploadFile(@RequestParam("file") MultipartFile file) {
        try {
            if (file.isEmpty()) {
                return ResultVO.error("文件不能为空");
            }

            String fileUrl = minioUtil.uploadFile(file);
            ResultVO<String> result = ResultVO.success(fileUrl);
            result.setMsg("文件上传成功");
            return result;
        } catch (Exception e) {
            log.error("文件上传失败", e);
            return ResultVO.error("文件上传失败: " + e.getMessage());
        }
    }

    /**
     * 合同列表查询page
     */
    @GetMapping("/list")
    public TableDataInfo<Contract> listPage(ContractDto contractDto){
         ContractVo list = contractService.listPage(contractDto);
        TableDataInfo<Contract> tableDataInfo = new TableDataInfo<>();
        tableDataInfo.setRows(list.getContracts());
        tableDataInfo.setCode(200);
        tableDataInfo.setMsg("查询成功");
        tableDataInfo.setTotal(list.getTotalCount());
        return tableDataInfo;
    }

    /**
     * 新增合同
     * @param contractDto
     * @return
     */
    @PostMapping
    public ResultVO saveContract(@RequestBody ContractDto contractDto){
        log.info("接收到的合同数据: {}", contractDto);
        log.info("fileName字段值: {}", contractDto.getFileName());
        contractService.saveContract(contractDto);
        return ResultVO.success();
    }

    /**
     * 合同详情
     */
    @GetMapping("/{id}")
    public ResultVO<Contract> getContractById(@PathVariable Long id){
        log.info("查询合同详情，合同ID: {}", id);
        Contract contract = contractService.getContractById(id);
        log.info("查询到的合同详情: {}", contract);
        ResultVO<Contract> result = ResultVO.success(contract);

        log.info("返回给前端的数据: {}", result);
        return result;
    }

    /**
     * 回显合同数据
     */
    @GetMapping("/detail/{id}")
    public ResultVO<Contract> ContractById(@PathVariable Long id){
        log.info("回显合同数据，合同ID: {}", id);
        Contract res = contractService.getContractById(id);
        log.info("回显的合同数据: {}", res);
        ResultVO<Contract> result = ResultVO.success(res);
        log.info("返回给前端的数据: {}", result);
        return result;
    }


    /**
     * 修改合同
     * @param contractDto
     * @return
     */
    @PutMapping
    public ResultVO updateContract(@RequestBody ContractDto contractDto){
        log.info("更新合同数据: {}", contractDto);
        log.info("更新时fileName字段值: {}", contractDto.getFileName());
        contractService.updateContract(contractDto);
        return ResultVO.success();
    }
}
